/**
 * <p>ajax辅助类</p>
 * xtype = cceafajaxhelper

 * @module Ext-ux-cceaf-util
 * @class cceaf.util.AjaxHelper
 * @extends Object
 * @static
 * @since 1.0
 */
Ext.ns('cceaf.util');

Ext.define('cceaf.util.AjaxHelper',{
	//extjs cmd只能通过类查找，需定义空类
});

cceaf.util.AjaxHelper = (function() {
	
	return {
		
		/**
		 * 根据组织ID得到组织地址信息
		 * 
		 * @param {Number} groupid 组织ID
		 * @return {String} 组织机构所在的联系地址信息
		 */
		getGroupAddress: function(groupid){
			var groupAddress = "";
			if(groupid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!groupAddress.action',
					async: false,
					success: function(response){
						var responseText = response.responseText.ltrim();
						var json = Ext.decode(responseText);
						if (json.success) {
							groupAddress = json.message;
							return groupAddress;
						} else {
							cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
							groupAddress = "";
							return groupAddress;
						}
					},
					params: {groupid:groupid}
				});
		    }
			return groupAddress;
		},
		/**
		 * 根据组织ID得到组织名称
		 * 
		 * @param {Number} groupid 组织ID
		 * @return {String} 组织名称
		 */
		getGroupName: function(groupid){
			var groupName = "";

			if(groupid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!getGroupName.action',
					async: false,
					success: function(response){
						var responseText = response.responseText.ltrim();
						var json = Ext.decode(responseText);
						if (json.success) {
							groupName = json.message;
							return groupName;
						} else {
							cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
							groupName = "";
							return groupName;
						}
						
					},
					//timeout: 100,
					params: {groupid:groupid}
				});
		    }
			
			return groupName;
		},
		
		/**
		 * 根据上级组织parentid得到下级组织最大编号
		 * 
		 * @param {Number} parentid 上级组织ID
		 * @return {String} 组织名称
		 */
		getMaxCodeOfNextLever: function(parentid){
			var code = "";

			if(parentid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!getMaxCodeOfNextLever.action',
					async: false,
					success: function(response){
						var responseText = response.responseText.ltrim();
						var json = Ext.decode(responseText);
						if (json.success) {
							code = json.message;
							return code;
						} else {
							cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
							code = "";
							return code;
						}
					},
					//timeout: 100,
					params: {parentid: parentid}
				});
		    }
			
			return code;
		},
		
		/**
		 * 根据地区代码得到地区名称
		 * 
		 * @param {String} districtId 地区代码
		 * @return {String} 地区名称
		 */
		getDistrictName: function(districtId){
			var districtName = "";

			if(districtId != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!getDistrictName.action',
					async: false,
					success: function(response){
						var responseText = response.responseText.ltrim();
						var json = Ext.decode(responseText);
						if (json.success) {
							districtName = json.message;
							return districtName;
						} else {
							cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
							districtName = "";
							return districtName;
						}
					},
					//timeout: 100,
					params: {districtId: districtId}
				});
		    }
			
			return districtName;
		},
		
		/**
		 * 根据部门ID得到部门名称
		 * 
		 * @param {Number} deptid 部门ID
		 * @return {String} 部门名称
		 */
		getDeptName: function(deptid){
			var deptName = "";

			if(deptid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!getDeptName.action',
					async: false,
					success: function(response){
						var responseText = response.responseText.ltrim();
						var json = Ext.decode(responseText);
						if (json.success) {
							deptName = json.message;
							return deptName;
						} else {
							cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
							deptName = "";
							return deptName;
						}
					},
					//timeout: 100,
					params: {deptid:deptid}
				});
		    }
			
			return deptName;
		},

		/**
		 * 根据代码类型和代码值得到代码名称
		 * 
		 * @param {String} codetype 代码类型
		 * @param {String} codevalue 代码值
		 * @param {String} queryMode 查询模式，缺省为local，从服务器区用remote
		 * @return {String} 代码名称
		 */
		getCodeName: function(codetype,codevalue,queryMode){
			var codename = "";
			
			if(codevalue != null){  
				if (queryMode && queryMode == 'remote') {
					Ext.Ajax.request({
						url: 'pageDataProvider!getCodeName.action',
						async: false,
						success: function(response){
							var responseText = response.responseText.ltrim();
							var json = Ext.decode(responseText);
							if (json.success) {
								codename = json.message;
								return codename;
							} else {
								cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
								codename = "";
								return codename;
							}
						},
						//timeout: 100,
						params: {codetype:codetype,codevalue:codevalue}
					});
				} else {
					var json = eval('_jsonCode.' + codetype);
					Ext.Array.each(json, function(code) {
						
						value = code.value || code.get('value');
						if (codevalue == value) {
							codename = code.name || code.get('name');
							
							return false; //退出循环
						}
						/*
						value = code.value;
						if (codevalue == value) {
							codename = code.name;
							
							return false; //退出循环
						}*/
					});
				
				}
		    }
			
			return codename;
		},

		/**
		 * 根据配置代码类型和代码值得到配置代码名称
		 * 
		 * @param {String} codetype 代码类型
		 * @param {String} codevalue 代码值
		 * @param {String} queryMode 查询模式，缺省为local，从服务器区用remote
		 * @return {String} 代码名称
		 */
		getCodeConfigName: function(codetype,codevalue,queryMode){
			var codename = "";
			
			if(codevalue != null){  
				if (queryMode && queryMode == 'remote') {
					Ext.Ajax.request({
						url: 'pageDataProvider!getCodeName.action',
						async: false,
						success: function(response){
							var responseText = response.responseText.ltrim();
							var json = Ext.decode(responseText);
							if (json.success) {
								codename = json.message;
								return codename;
							} else {
								cceaf.util.ExtHelper.showErrorDialog(json.message, json.url);
								codename = "";
								return codename;
							}
						},
						//timeout: 100,
						params: {codetype:codetype,codevalue:codevalue}
					});
				} else {
					var json = eval('_jsonCodeConfig.' + codetype);
					Ext.Array.each(json, function(code) {
						//alert(code.value);
						value = code.value || code.get('value');
						if (codevalue == value) {
							codename = code.name || code.get('name');
							
							return false; //退出循环
						}
					});
				
				}
		    }
			
			return codename;
		},
		
		/**
		 * 根据用户ID得到用户的角色数组
		 * 
		 * @param {String} userid 用户编号
		 * @return {Object[]} 角色ID的数组
		 */
		findRoleListByUserid: function(userid){
			var roles = [];
			
			if(userid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!findRoleListByUserid.action',
					async: false,
					success: function(response){
						var roleList = Ext.JSON.decode(response.responseText);
						for(var i=0;roleList != null && i<roleList.length;i++){
							roles[i] = roleList[i].roleid;
						}
					},
					//timeout: 100,
					params: {userid:userid}
				});
		    }
			
			return roles;
		},

		/**
		 * 根据组织ID得到组织的角色数组
		 * 
		 * @param {Number} groupid 组织ID
		 * @return {Object[]} 角色ID的数组
		 */
		findRoleListByGroupid: function(groupid){
			var roles = [];
			
			if(groupid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!findRoleListByGroupid.action',
					async: false,
					success: function(response){
						var roleList = Ext.JSON.decode(response.responseText);
						for(var i=0;roleList != null && i<roleList.length;i++){
							roles[i] = roleList[i].roleid;
						}
					},
					//timeout: 100,
					params: {groupid:groupid}
				});
		    }
			
			return roles;
		},

		/**
		 * 根据角色ID得到角色的组织机构
		 * 
		 * @param {Number} roleid 角色ID
		 * @return {Object[]} 组织ID的数组
		 */
		findGroupListByRoleid: function(roleid){
			var groups = [];
			
			if(roleid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!findGroupListByRoleid.action',
					async: false,
					success: function(response){
						
						var groupList = Ext.JSON.decode(response.responseText);
						for(var i=0;groupList != null && i<groupList.length;i++){
							groups[i] = groupList[i].groupid;
						}
					},
					//timeout: 100,
					params: {roleid:roleid}
				});
		    }
			
			return groups;
		},

		/**
		 * 根据角色ID得到角色的用户
		 * 
		 * @param {Number} roleid 角色ID
		 * @return {Object[]} 用户ID的数组
		 */
		findUserListByRoleid: function(roleid){
			var users = [];
			
			if(roleid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!findUserListByRoleid.action',
					async: false,
					success: function(response){
						var userList = Ext.JSON.decode(response.responseText);
						for(var i=0;userList != null && i<userList.length;i++){
							users[i] = userList[i].userid;
						}
					},
					//timeout: 100,
					params: {roleid:roleid}
				});
		    }
			
			return users;
		},
		
		/**
		 * 根据用户组ID得到用户
		 * 
		 * @param {Number} ugid 用户组ID
		 * @return {Object[]} 用户ID的数组
		 */
		findUserListByUgid: function(ugid){
			var users = [];
			
			if(ugid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!findUserListByUgid.action',
					async: false,
					success: function(response){
						var userList = Ext.JSON.decode(response.responseText);
						for(var i=0;userList != null && i<userList.length;i++){
							users[i] = userList[i].userid;
						}
					},
					//timeout: 100,
					params: {ugid:ugid}
				});
		    }
			
			return users;
		},
		
		
		/**
		 * 根据sysmanager节点ID得到节点名称
		 * 
		 * @param nodeid 节点ID
		 * @return 节点名称
		 */
		getNodeNameByBrowseConfig: function(nodeid){
			var nodeName = "";

			if(nodeid != null){  
				Ext.Ajax.request({
					url: 'pageDataProvider!getNodeNameByBrowseConfig.action',
					async: false,
					success: function(response){
						nodeName = response.responseText.ltrim();
						return nodeName;
					},
					params: {nodeid: nodeid}
				});
		  }

			return nodeName;
		}
		
	};
	
}());